package com.catosist.run.business.gestorvehiculo;

import com.catosist.run.dataaccess.domain.VehiculoDao;
import com.catosist.run.dataaccess.model.ColoresVO;
import com.catosist.run.dataaccess.model.MarcasVO;
import com.catosist.run.dataaccess.model.VehiculoVO;
import com.catosist.run.services.VehiculoDTO;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 *
 * @author Bicho
 */
public class GestorVehiculoImpl implements GestorVehiculo {

    public GestorVehiculoImpl() {
        //VehiculoVO vehiculoVO= new VehiculoVO();
    }

    @Override
    public List<VehiculoDTO> getAll() {

        System.out.println("GestorVehiculoImpl: Obtener todas Vehiculos");
//        if (vehiculoVOs == null) {
        vehiculoVOs = vehiculoDao.getAll();
//        }
        List<VehiculoDTO> tmp = new LinkedList<VehiculoDTO>();
        for (Iterator<VehiculoVO> it = vehiculoVOs.iterator(); it.hasNext();) {
            VehiculoVO vehiculoVO = it.next();
            tmp.add(new VehiculoDTO(
                    vehiculoVO.getId(),
                    vehiculoVO.getColores().getNombre(),
                    vehiculoVO.getMarcas().getNombre(),
                    vehiculoVO.getFingreso(),
                    vehiculoVO.getModelo(),
                    vehiculoVO.getPlaca(),
                    vehiculoVO.getOrigen(),
                    vehiculoVO.getCombustible(),
                    vehiculoVO.getActivo()));
        }
        return tmp;
    }

    @Override
    public VehiculoDTO getByIndex(int indice) {
        VehiculoVO vehiculoVO = getVOatIndex(indice);
        return new VehiculoDTO(vehiculoVO.getColores().getNombre(),
                vehiculoVO.getMarcas().getNombre(),
                vehiculoVO.getFingreso(),
                vehiculoVO.getModelo(), 
                vehiculoVO.getPlaca(), vehiculoVO.getOrigen(),
                vehiculoVO.getCombustible(), vehiculoVO.getActivo());
    }

    @Override
    public VehiculoDTO getById(int id) {
        updateListVo();
        VehiculoVO tmp = null;
        System.out.println("Id del vehiculo"+id);

        for (Iterator<VehiculoVO> it = vehiculoVOs.iterator(); it.hasNext();) {
            VehiculoVO vehiculoVO = it.next();
            if (vehiculoVO.getId() == id) {
                tmp = vehiculoVO;
            }
        }

        return new VehiculoDTO(tmp.getColores().getNombre(),
                tmp.getMarcas().getNombre(),
                tmp.getFingreso(),
                tmp.getModelo(), 
                tmp.getPlaca(), tmp.getOrigen(),
                tmp.getCombustible(), tmp.getActivo());

    }

    @Override
    public int getIdAtIndex(int indice) {
        return getVOatIndex(indice).getId();
    }

    @Override
    public void saveOrUpdate(VehiculoDTO o) {

        VehiculoVO vehiculoVO = new VehiculoVO();
        if (o.getId() > 0) {
            vehiculoVO.setId(o.getId());

        }
        vehiculoVO.setActivo(o.getActivo());
        ColoresVO coloresVO = new ColoresVO();
        coloresVO.setId(o.getColorId());
        vehiculoVO.setColores(coloresVO);

        vehiculoVO.setCombustible(o.getCombustible());
        vehiculoVO.setFingreso(o.getFingreso());

        MarcasVO marcasVO = new MarcasVO();
        marcasVO.setId(o.getMarcaId());
        vehiculoVO.setMarcas(marcasVO);

        vehiculoVO.setModelo(o.getModelo());
        vehiculoVO.setOrigen(o.getOrigen());
        vehiculoVO.setPlaca(o.getPlaca());
        vehiculoDao.saveOrUpdate(vehiculoVO);

    }

    @Override
    public void deleteById(int i) {
        VehiculoVO vehiculoVO = vehiculoDao.get(i);
        if (vehiculoVO.getViajes().size() > 0 || vehiculoVO.getMantenimientos().size() > 0) {
            vehiculoVO.setActivo(false);
            vehiculoDao.update(vehiculoVO);
        } else {
            vehiculoDao.delete(vehiculoVO);
        }
    }

    private VehiculoVO getVOatIndex(int indice) {
        return this.vehiculoVOs.get(indice);
    }

    public VehiculoDao getVehiculoDao() {
        return this.vehiculoDao;
    }

    public void setVehiculoDao(VehiculoDao vehiculoDao) {
        this.vehiculoDao = vehiculoDao;
    }
    private List<VehiculoVO> vehiculoVOs;
    private VehiculoDao vehiculoDao;

    @Override
    public void updateListVo() {
        vehiculoVOs = vehiculoDao.getAll();
    }

    public int getIndexOf(int voId) {
        for (int i = 0; i < vehiculoVOs.size(); i++) {
            VehiculoVO vehiculoVO = vehiculoVOs.get(i);
            if (vehiculoVO.getId() == voId) {
                return i;
            }
        }
        return 0;
    }

    public void deleteByIndex(int index) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
